home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / sound / rukc10.zip / X03.C < prev    next >
C/C++ Source or Header  |  1993-03-06  |  7KB  |  250 lines

  1.  
  2. #include <dos.h>
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdarg.h>
  7.  
  8. #include "ruckdac.h"
  9.  
  10. /*
  11. X03.c 28-Feb-93 chh
  12. Record from device to DOS memory or XMS
  13. */
  14.  
  15. /*
  16. The following structures are in ruckdac.h
  17. */
  18.  
  19. extern struct DacDataArea __pascal DACDATA;
  20.  
  21. struct SysInfoPack SIP;
  22. struct InitPack IP;
  23. struct XitPack XP;
  24. struct LoadPack LP;
  25. struct SetPack SP;
  26. struct SetProPack SPP;
  27. struct PlaybackPack PBP;
  28. struct RecordPack RP;
  29. struct DeallocPack DP;
  30.  
  31.  
  32. int rez, rez2;      /* result status codes */
  33. char nums[9] = {7}; /* number buffer for _cgets()*/
  34. char filename[81];  /* pathname to load */
  35.  
  36.  
  37. int pick_device(int *devID, int *XMSflag)
  38. {
  39.  
  40.     int td=0;
  41.  
  42.     SIP.Func = SysInfoDac;
  43.     rez = RUCKDAC(&SIP);
  44.     if (rez == 0) {
  45.         printf("CPU is a %u/%u\n",SIP.CPU,SIP.MHz);
  46.  
  47.         printf("\n0. End program");
  48.         if (SIP.SD[4].device)
  49.             printf("\n5. Sound Blaster, port %xh",SIP.SD[4].Port);
  50.         if (SIP.SD[5].device)
  51.             printf("\n6. Sound Blaster Pro, port %xh",SIP.SD[5].Port);
  52.         if (SIP.SD[4].device)
  53.             printf("\n\n7. Sound Blaster as 5 but use XMS (if applicable)");
  54.         if (SIP.SD[5].device)
  55.             printf("\n8. Sound Blaster Pro as 6 but use XMS (if applicable)\n");
  56.  
  57.         printf("\nSelection: ");
  58.         td = atoi(_cgets(nums));
  59.         td--;   /* since devices are numbered 0 to 5 */
  60.  
  61.         if (td > 6) {
  62.             *XMSflag = -1;      /* XMS memory selected with SB */
  63.                                 /* use -1 so as to auto-alloc below */
  64.             td-=2;              /* map to appropriate device */
  65.         }
  66.  
  67.         if ((td >=4) && (td <=5)) { /* validate device selected is available */
  68.             if (SIP.SD[td].device == 0)
  69.                 td = -1;
  70.         }
  71.         else
  72.             td = -1;
  73.     }
  74.  
  75.     *devID = td;
  76.     return(rez);
  77. }
  78.  
  79.  
  80. int init_device(int devID)
  81. {
  82.     /*
  83.     Initialize RUCKDAC and device and register ExitDac with _atexit
  84.     */
  85.  
  86.     IP.Func = InitDac;
  87.     IP.DeviceID = devID;
  88.     IP.IOport = SIP.SD[devID].Port;
  89.     IP.IRQline = SIP.SD[devID].IRQ;
  90.     IP.DMAch = SIP.SD[devID].DMA;
  91.  
  92.     rez = RUCKDAC(&IP);                 /* Initialize */
  93.     if (rez == 0) {
  94.         XP.Func = AtExitDac;
  95.         rez2 = RUCKDAC(&XP);
  96.         if (rez2 != 0) {
  97.             printf("AtExitDac failed, press Enter to continue");
  98.             getchar();
  99.         }
  100.  
  101.  
  102.         /*
  103.         Increase SB Pro main and vol volumes to max
  104.         */
  105.  
  106.         if (devID == 5) {
  107.             SPP.Func = SetVolMainSBP;
  108.             SPP.Volume = 0x0F0F;
  109.             rez2 = RUCKDAC(&SPP);
  110.             SPP.Func = SetVolVocSBP;
  111.             SPP.VolVoc = 0x0F0F;
  112.             rez2 = RUCKDAC(&SPP);
  113.         }
  114.     }
  115.     return(rez);
  116. }
  117.  
  118.  
  119. int main()
  120. {
  121.  
  122.     int devID=-1, XMSflag = 0;
  123.     unsigned int RecSecs=5, RecRate=12000;
  124.  
  125.  
  126.     printf("X03.C - RUCKUS-DAC record to memory example. [930228]\n");
  127.  
  128.     rez = pick_device(&devID, &XMSflag);
  129.     if (devID >= 0) {
  130.         printf("Initializing devID %u\n",devID);
  131.         rez = init_device(devID);
  132.  
  133.         /*
  134.         The following record example source is coded inline here
  135.         to simply readability -- but it's so easy to add things that I just
  136.         kept adding stuff, so take it slow if you don't follow at first
  137.         */
  138.  
  139.         if (rez == 0) {
  140.  
  141.             /*
  142.             Select input source
  143.             */
  144.  
  145.             SPP.Func = SetSourceSBP;
  146.             SPP.SourceIn = 0;       /* 0=mic,1=CD,2=line */
  147.             rez = RUCKDAC(&SPP);    /* should always check rez status */
  148.                                     /* (not like I'm doing here!) */
  149.  
  150.             printf("   Device ID: %u\n",devID);
  151.             printf(" Record from: mic\n");
  152.             printf(" Record time: %u secs\n",RecSecs);
  153.             printf(" Record rate: %u Hz\n",RecRate);
  154.  
  155.             /*
  156.             Following is memory available for record. We don't use
  157.             it (but should) and just record for 5 secs at 12kHz (~60K)
  158.             */
  159.  
  160.             if (XMSflag == 0) {
  161.                 printf(" Memory type: DOS\n");
  162.                 printf("K bytes free: %u\n",DACDATA.MemDOS);
  163.             }
  164.             else {
  165.                 printf(" Memory type: XMS\n");
  166.                 printf("K bytes free: %u\n",DACDATA.MemXMM);
  167.             }
  168.  
  169.             /*
  170.             Prepare for record (5 secs at 12000Hz sample rate)
  171.             */
  172.  
  173.             RP.Func = RecordDac;
  174.             RP.SampleRate = RecRate;
  175.             RP.XMMhandle = XMSflag;   /* if -1 auto-alloc an XMS handle */
  176.             RP.RecordPtr = NULL;      /* if DOS mem then auto-alloc it */
  177.                                       /* also used to return ptr after Rec*/
  178.  
  179.             /* RecordBytes limit is 16MB (VOC block limit) */
  180.  
  181.             RP.RecordBytes = ((long)RecRate * (long)RecSecs);
  182.             RP.StereoFlag = 0;  /* recording from mic is mono */
  183.  
  184.             printf("\nPress Enter to begin recording...");
  185.             gets(filename);
  186.  
  187.             rez = RUCKDAC(&RP);
  188.  
  189.             /*
  190.             Since record is done as a background DMA task, we can do just
  191.             about anything we want while the recording is taking place.
  192.             Here, I'll just wait until the recording is over
  193.             */
  194.  
  195.             do                          /* I don't think DACDATA needs a  */
  196.                 ;
  197.             while (DACDATA.End == 0);   /* VOLATILE... CONST... both... ? */
  198.  
  199.             /*
  200.             Recording over. Play it back.
  201.             */
  202.  
  203.             printf("Recorded %lu bytes, press Enter to playback recording...",DACDATA.RecordLen);
  204.             gets(filename);
  205.  
  206.             PBP.Func = PlayDac;
  207.             PBP.Mode = 2;
  208.             if (XMSflag == 0) {
  209.                 PBP.XMMhandle = 0;
  210.                 PBP.LoadPtr = RP.RecordPtr;
  211.             }
  212.             else {
  213.                 PBP.XMMhandle = RP.XMMhandle;
  214.                 PBP.LoadPtr = NULL;
  215.             }
  216.             rez = RUCKDAC(&PBP);
  217.  
  218.             do                          /* hang around until it's done */
  219.                 ;
  220.             while (DACDATA.End == 0);
  221.  
  222.             XP.Func = EndDac;           /* end play */
  223.             rez = RUCKDAC(&XP);
  224.  
  225.             /*
  226.             Release memory used by RecordDac (ExitDac would do that, too)
  227.             */
  228.  
  229.             DP.Func = DeallocDac;
  230.             if (XMSflag == 0) {
  231.                 DP.HandSeg = _FP_SEG(RP.RecordPtr);
  232.                 DP.TypeFlag = 0;
  233.             }
  234.             else {
  235.                 DP.HandSeg = RP.XMMhandle;
  236.                 DP.TypeFlag = 1;
  237.             }
  238.         }
  239.         else
  240.             printf("Initialization failed, %u\n", rez);
  241.     }
  242.     else
  243.         puts("Device pick failed");
  244.  
  245. XP.Func = ExitDac;
  246. rez = RUCKDAC(&XP);
  247. return(rez);
  248.  
  249. }
  250.